require 'activerecord'

module Eusden

  module StringInstanceExtensions
    def canonize
      gsub(' & ', ' and ').
      gsub(/\s+/,'-').
      gsub(/[^-\w]+/, '').
      gsub(/-+/, '-').
      downcase
    end
  end
  
  module SharedExtensions
    def cleaned_page(page)
      page.to_i > 0 ? page.to_i : 1
    end
  end
  
  module ActiveRecordBaseExtensions
    def find_with_catch_not_found *args
      find_without_catch_not_found *args
    rescue ActiveRecord::RecordNotFound
      nil
    end
  end

end

String.send(:include, Eusden::StringInstanceExtensions)
ActiveRecord::Base.send(:extend, Eusden::SharedExtensions)
ActiveRecord::Base.send(:extend, Eusden::ActiveRecordBaseExtensions)
ActiveRecord::Base.class_eval do 
  class << self
    alias_method_chain :find, :catch_not_found
  end
end
